package cn.yanglq.Rss.web.controller.retrievepwd;

import cn.yanglq.Rss.web.common.vo.ApiResult;
import cn.yanglq.Rss.web.controller.BaseController;
import cn.yanglq.Rss.web.dao.UserMapper;
import cn.yanglq.Rss.web.model.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

/**
 * Created by yanglq on 2018/1/19.
 */
@Controller
@RequestMapping("/updatepwd111")
public class UpdatePwdController222 extends BaseController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/validPwd")
    @ResponseBody
    public ApiResult validPwd(
            @RequestParam(value = "oldpasswd", required = true) String oldpasswd) {
        ApiResult result = new ApiResult();
        String loginid = findCurrentLoginid();
        User user = userMapper.selectByPrimaryKey(loginid);
        if (user != null && Objects.equals(user.getPasswd(), DigestUtils.md5Hex(oldpasswd))) {
            result.setErrorcode(0);
            result.setSuccess(true);
            result.setMessage("验证通过！");
        } else {
            result.setErrorcode(-1);
            result.setSuccess(false);
            result.setMessage("请输入正确密码");
        }
        return result; //校验通过返回成功
    }


    @RequestMapping(value = "/update")
    @ResponseBody
    public ApiResult update(@RequestParam(value = "passwd", required = true) String passwd) {

        ApiResult result = new ApiResult();
        String loginId = findCurrentLoginid();
        User user = userMapper.selectByPrimaryKey(loginId);
        if (user != null) {
            user.setPasswd(passwd);
            userMapper.updateByPrimaryKey(user);
            result.setErrorcode(0);
            result.setSuccess(true);
            result.setMessage("修改成功！");
            return result;
        }
        result.setErrorcode(1);
        result.setSuccess(false);
        result.setMessage("修改失败！");
        return result;
    }
}
